import {Form,Table,Button,Progress,Modal,Alert,Icon,Row,Col,Card} from 'antd'
import {WeaTop,WeaInput4SapDs} from 'weaCom'

import { Upload, message } from 'antd';

const FormItem = Form.Item;

import * as API from '../api/util'

const columns = [{
  title: '名称',
  dataIndex: '',
  key: 'name',
  render(text,record,index) {
  	return (
  		<div>[{record.id}] {record.workflowname}</div>
  	)
  }
}, {
  title: '浏览按钮',
  dataIndex: '',
  key: 'browser',
  render(text,record,index) {
  	const {db_datashowset,xml_Browser} = record.counts;
  	const count = parseInt(db_datashowset||0)+parseInt(xml_Browser||0);
  	return (
  		<div>{count}</div>
  	)
  }
}, {
  title: 'SAP浏览按钮',
  dataIndex: '',
  key: 'sapbrowser',
  render(text,record,index) {
  	const {db_intBrowserbaseInfo,xml_SapBrowser} = record.counts;
  	const count = parseInt(db_intBrowserbaseInfo||0)+parseInt(xml_SapBrowser||0);
  	return (
  		<div>{count}</div>
  	)
  }
}, {
	title: '节点后ACTION',
	dataIndex: '',
	key: 'action',
	render(text,record,index) {
		const {db_workflowactionset} = record.counts;
		const count = parseInt(db_workflowactionset||0);
		return (
			<div>{count}</div>
		)
	}
}];

class Main extends React.Component {
	constructor(props) {
		super(props);
		this.state = {
			data:{
				datas:[],
				details:[]
			},
			loading:false,
			current:0,
			url:"",
			checkKeys:[]
		}
	}
	render() {
		let that = this;
		const {history} = this.props;
		const {getFieldValue,getFieldProps} = this.props.form;
		const {data,loading,current,url,checkKeys} = this.state; 
		const {datas,details} = data;
		const uploadProps = {
		  	name:"zipFile",
			showUploadList:true,
			action:'/cloudstore/system/ControlServlet.jsp?action=Action_DoDiffXmlImport',
			data:{},
			onChange(info) {
				console.log("info:",info);
				const {status,response} = info.file;
				console.log("status:",status," response:",response);
				if(status=="done"&&response.status=="true") {
					that.setState({
						url:response.url
					});
					that.doImportCheck(response.url);
				}
			}
		};
		const rowSelection = {
	      	checkKeys,
	      	onChange(selectedRowKeys) {
	      		console.log("selectedRowKeys:",selectedRowKeys);
	      		that.setState({
	      			checkKeys:selectedRowKeys
	      		})
	      	}
	    };
		//console.log("url:",url);
		return (
			<div className="wea-con" style={{background:"url('/cloudstore/app/no0000086/images/brillant.png') repeat scroll 0 0 transparent"}}>
				<div>
				<Form horizontal>
					<Row>
						<Col span={16} offset={1}>
							<FormItem wrapperCol={{ span:16, offset: 6 }}>
								<div style={{fontSize:22,fontWeight:600}}>流程跨环境导入</div>
							 </FormItem>
							<FormItem label="选择流程文件"
								labelCol={{ span: 6 }}
	          					wrapperCol={{ span: 16 }}
	          					required>
					          	<Upload {...uploadProps}>
								    <Button type="ghost">
								      <Icon type="upload" /> 点击上传
								    </Button>
								</Upload>
					        </FormItem>
					        <FormItem label="已选清单"
								labelCol={{ span: 6 }}
	          					wrapperCol={{ span: 16 }}
	          					required>
	          					<Table  dataSource={details} columns={columns} rowSelection={rowSelection} rowKey={record => record.id} size="small" />
	          				</FormItem>
	          				{this.haveSapBrowser() &&
	          					(
	          						<FormItem label="SAP数据源"
									labelCol={{ span: 6 }}
		          					wrapperCol={{ span: 6 }}
		          					required>
	          							<WeaInput4SapDs {...getFieldProps('sapDs',{initialValue:""})} />
	          						</FormItem>
	          					)
	          				}
					        <FormItem wrapperCol={{ span:16, offset: 6 }} style={{ marginTop: 24 }}>
					        	<div>
					        		<Button type="primary" disabled={!url || url=="" || checkKeys.length===0 || (this.haveSapBrowser()&&!getFieldValue("sapDs"))} 
					        			onClick={this.doImport.bind(this,url)} loading={loading}>导入</Button>
					        		<Button type="ghost" style={{marginLeft:15}} onClick={()=>{history.pushState(null,"/");}}>返回</Button>
					        	</div>
					        </FormItem>
					        <FormItem wrapperCol={{ span:10, offset: 6 }} style={{ marginTop: 24 }}>
					        	<Progress percent={current} />
					        </FormItem>
					        {url && 
					        <FormItem wrapperCol={{ span:16, offset: 6 }} style={{ marginTop: 24 }}>
					        	<Alert
								    message="成功上传文件"
								    description={
								    	<div>
								    		<p>请填写信息后点击导入！</p>
								    	</div>
								    }
								    type="info"
								    showIcon
								  />
					        </FormItem>}
					        <FormItem wrapperCol={{ span:16, offset: 6 }} style={{ marginTop: 24 }}>
					        	<Alert
								    message="说明"
								    description={
								    	<div>
								    	<p>1、此程序用于流程本环境导出，并在其他环境导入</p>
								    	<p>2、除了导出【流程】，此程序还将导出【浏览按钮】、【SAP浏览按钮】</p>
								    	</div>
								    }
								    type="info"
								    showIcon
								  />
					        </FormItem>
						</Col>
						<Col span={8} offset={4} style={{paddingTop:30}}>
						</Col>
					</Row>
				</Form>
				</div>
			</div>
		)
	}
	doImportCheck(url) {
		let that = this;
		API.doImportCheck({path:url}).then((data)=>{
			console.log("data:",data);
			that.setState({
				data:data
			});
		});
	}
	doImport(url) {
		const {checkKeys} = this.state;
		const {getFieldValue} = this.props.form;
		let that = this;
		this.setState({
			loading:true,
			current:0,
			url:""
		});

		let current = 0;
		let run = setInterval(()=>{
			if(current==100) current = 99;
			
			that.setState({
				current:current++
			});
			
		},100);
		// setTimeout(()=>{
		// 	that.setState({
		// 		current:100,
		// 		loading:false
		// 	});
		// 	window.clearInterval(run);
		// },10000);
		console.log("url:",url);
		API.doImport({path:url,wfId:checkKeys.join(","),sapds:getFieldValue("sapDs")}).then((msg)=>{
			console.log("msg:",msg);
			setTimeout(()=>{
				that.setState({
					loading:false,
					current:100,
					url:msg.url
				});
				window.clearInterval(run);
				that.doImportCheck(url);
			},3000);
		});
	}
	haveSapBrowser() {
		let find = false;
		const {details} = this.state.data;
		for(let i=0;i<details.length&&!find;i++) {
			const counts = details[i].counts;
			if(counts.db_intBrowserbaseInfo>0||counts.xml_SapBrowser>0) {
				find = true;
			}
		}
		//console.log("find:",find);
		return find;
	}
}

Main = Form.create()(Main);

export default Main

//<Button type="ghost" style={{marginLeft:15}}>日志</Button>